﻿using ACC.Data.Entities.Common;
using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using ACC.Extensions.Utilities;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for ConfigProducePlanWnd.xaml
    /// </summary>
    public partial class ConfigProducePlanWnd : MetroWindow
    {
         public ConfigProducePlanWnd(AccProcess accProcess)
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
            _accProc = accProcess;
        }
        private AccProcess _accProc;
        private List<ClientWorkOrder> _listWorkOrder = new List<ClientWorkOrder>();
        public List<ClientWorkOrder> ListWorkOrde
        {
            get
            {
                return _listWorkOrder;
            }
            set
            {
                value = _listWorkOrder;
            }
        }
        private void BaseCustomWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (UISession.PartTypeList != null)
            {
                this.cmbPartNo.ItemsSource = UISession.PartTypeList;
                this.cmbPartNo.SelectedValuePath = "PartNO";
                this.cmbPartNo.DisplayMemberPath = "PartNO";
            }
            RefreshDateNo();
            dpDT_SelectedDateChanged(null, null);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            ClientWorkOrder selWorkOrder = (ClientWorkOrder)dbgProducePlan.SelectedItem;
            _listWorkOrder.Remove(selWorkOrder);
            LoadDataGridData(_listWorkOrder);
            RefreshDateNo();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (CheckAllInput())
            {
                ClientWorkOrder workOrder = new ClientWorkOrder();
                workOrder.WONO = this.txtWorkNO.Text.ToString().Trim();
                workOrder.PartNo = this.cmbPartNo.SelectedValue == null ? "" : cmbPartNo.SelectedValue.ToString().Trim();
                workOrder.PlanQuantity = this.txtPlanQty.Text.ToString().Trim();
                _listWorkOrder.Add(workOrder);
                LoadDataGridData(_listWorkOrder);
            }
            RefreshDateNo();
        }
        private void btnEmpty_Click(object sender, RoutedEventArgs e)
        {
            RefreshDateNo();
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if(ListWorkOrde!=null)
            {
                DateTime dt = this.dpDT.SelectedDate.Value.Date;
                ClientResponse accRes =  _accProc.SaveWorkOrder(dt, ListWorkOrde);
                if(accRes.Result== false)
                {
                    MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), accRes.Message);
                }
            }
        }

        private bool CheckAllInput()
        {
            if (string.IsNullOrEmpty(this.txtWorkNO.Text.ToString().Trim()))
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "不能为空");
                this.txtWorkNO.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(this.cmbPartNo.SelectedValue ==null?"":cmbPartNo.SelectedValue.ToString().Trim()))
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "不能为空");
                this.cmbPartNo.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(this.txtPlanQty.Text.ToString().Trim()))
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "不能为空");
                this.txtPlanQty.Focus();
                return false;
            }
            return true;
        }
        private void RefreshDateNo()
        {
            if (this.dpDT.SelectedDate ==null)
            {
                this.txtWorkNO.Text = "WO" + System.DateTime.Now.ToString("yyyyMMdd");
            }
            else
            { 
                this.txtWorkNO.Text = "WO" + this.dpDT.SelectedDate.Value.ToString("yyyyMMdd");
            }
        }

        private void dbgProducePlan_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.txtWorkNO.Text = ((ClientWorkOrder)dbgProducePlan.SelectedItem).WONO;
            this.cmbPartNo.SelectedValue = ((ClientWorkOrder)dbgProducePlan.SelectedItem).PartNo;
            this.txtPlanQty.Text = ((ClientWorkOrder)dbgProducePlan.SelectedItem).PlanQuantity;
        }

        private void dpDT_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            RefreshDateNo();
            if (_accProc!=null)
            {
                _listWorkOrder = new List<ClientWorkOrder>();
                ClientResponse accRes = _accProc.GetWorkOrder(this.dpDT.SelectedDate.Value.Date);
                if (accRes.Result)
                {
                    if (accRes.Data != null)
                    {
                        _listWorkOrder = (List<ClientWorkOrder>)accRes.Data;

                    }
                }
                else
                {
                    MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), accRes.Message);
                }
                LoadDataGridData(_listWorkOrder);
            }
        }
        private void LoadDataGridData(List<ClientWorkOrder> listWorkOrder)
        {
            this.dbgProducePlan.ItemsSource = null;
            this.dbgProducePlan.ItemsSource = _listWorkOrder;
        }

        #region 输入校验
        //IsDigit是否是数字
        protected bool IsNumberic(string strInput)
        {
            if (string.IsNullOrEmpty(strInput))
                return false;
            foreach (char c in strInput)
            {
                if (!char.IsDigit(c))
                    return false;
            }
            return true;
        }
        //IsLetter 是否为字母
        protected bool IsCharLetter(string strInput)
        {
            if (string.IsNullOrEmpty(strInput))
                return false;
            foreach (char c in strInput)
            {
                if (!char.IsLetter(c))
                    return false;
            }
            return true;
        }
        protected void TextBoxIsNum_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        protected void TextBoxIsNum_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!IsNumberic(e.Text))
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        protected void TextBoxIsNum_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsNumberic(text))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }

        protected void TextBoxIsChar_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        protected void TextBoxIsChar_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!IsCharLetter(e.Text))
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        protected void TextBoxIsChar_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsCharLetter(text))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }



        #endregion

        private void MetroWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }
    }
}
